/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 * <p>
 * https://www.renren.io
 * <p>
 * 版权所有，侵权必究！
 */

package com.cn.xiaonuo.tcrfinance.tcrtimes.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cn.xiaonuo.common.entity.BusinessMessage;
import com.cn.xiaonuo.tcrfinance.tcrtimes.entity.PvHoliday;
import com.cn.xiaonuo.tcrfinance.tcrtimes.entity.PvTradeTime;
import com.cn.xiaonuo.tcrfinance.tcrtimes.mapper.HtTimeMapper;
import com.cn.xiaonuo.tcrfinance.tcrtimes.mapper.PvhtHolidayMapper;
import com.cn.xiaonuo.tcrfinance.tcrtimes.mapper.PvTradeTimeMapper;
import com.cn.xiaonuo.tcrfinance.tcrtimes.service.HtTimeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.text.SimpleDateFormat;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Service
public class HtTimeServiceImpl implements HtTimeService {

    @Autowired
    private HtTimeMapper htTimeDao;

    @Autowired
    private PvhtHolidayMapper pvHolidayMapper;

    @Autowired
    private PvTradeTimeMapper pvTradeTimeMapper;


    @Override
    public BusinessMessage selectHolidays(Map<String, Object> params) {
        BusinessMessage message = new BusinessMessage();
        try {

            Integer pageSize = Integer.valueOf(params.get("pageSize")+"");
            Integer pageNum = Integer.valueOf(params.get("pageNo")+"");
            if (null == pageNum || Integer.valueOf(pageNum) <= 1) {
                pageNum = 1;
            }
            if (null == pageSize || Integer.valueOf(pageSize) <= 1) {
                pageSize = 10;
            }
            // 设置分页参数
            PageHelper.startPage(pageNum, pageSize);
            String today = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            List<Map<String, Object>> list = htTimeDao.selectHolidays(today);
            PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(list);
            if (list.isEmpty()) {
                message.setCode("-1");
                message.setSuccess(false);
                message.setMessage("操作失败");
                return message;
            }
            message.setSuccess(true);
            message.setData(pageInfo);
            message.setMessage("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return message;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public BusinessMessage insertHoliday(Map<String, Object> params) {
        BusinessMessage message = new BusinessMessage();
        try {
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.setEntity(new PvHoliday() {{
                setHoliday((String) params.get("holiday"));
            }});
            PvHoliday pvHoliday = pvHolidayMapper.selectOne(queryWrapper);
            if (null != pvHoliday) {
                message.setCode("-1");
                message.setSuccess(false);
                message.setMessage("已是非工作日");
                return message;
            }
            PvHoliday holiday = new PvHoliday();
            holiday.setHoliday((String) params.get("holiday"));
            holiday.setCreateTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            LocalDateTime localDateTime = LocalDateTime.now();
            holiday.setUpdateTime(Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant()));
            pvHolidayMapper.insert(holiday);
            message.setSuccess(true);
            message.setCode("200");
            message.setMessage("操作成功");
        } catch (Exception e) {
            message.setCode("-1");
            message.setSuccess(false);
            message.setMessage("操作失败");
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            e.printStackTrace();
        }
        return message;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public BusinessMessage deleteHoliday(Map<String, Object> params) {
        BusinessMessage message = new BusinessMessage();
        try {
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.setEntity(new PvHoliday() {{
                setId(Long.valueOf(params.get("id")+""));
            }});
            PvHoliday holiday = pvHolidayMapper.selectOne(queryWrapper);
            if (null == holiday) {
                message.setCode("-1");
                message.setSuccess(false);
                message.setMessage("操作失败");
                return message;
            }
            LocalDate localDate = LocalDate.parse(holiday.getHoliday(), DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            DayOfWeek dayOfWeek = localDate.getDayOfWeek();
            if (dayOfWeek == DayOfWeek.SUNDAY) {
                message.setCode("-1");
                message.setSuccess(false);
                message.setMessage("星期天不能交易，不能删除");
                return message;
            }
            QueryWrapper queryWrapper2 = new QueryWrapper();
            queryWrapper2.setEntity(holiday);
            pvHolidayMapper.delete(queryWrapper2);
            message.setSuccess(true);
            message.setCode("200");
            message.setMessage("操作成功");
        } catch (Exception e) {
            message.setCode("-1");
            message.setSuccess(false);
            message.setMessage("操作失败");
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            e.printStackTrace();
        }
        return message;
    }

    @Override
    public BusinessMessage selectTradeTimes(Map<String, Object> params) {
        BusinessMessage message = new BusinessMessage();
        try {
            Integer pageSize = Integer.valueOf( params.get("pageSize")+"");
            Integer pageNum = Integer.valueOf( params.get("pageNo")+"");
            if (null == pageNum || Integer.valueOf(pageNum) <= 1) {
                pageNum = 1;
            }
            if (null == pageSize || Integer.valueOf(pageSize) <= 1) {
                pageSize = 10;
            }
            PageHelper.startPage(pageNum, pageSize);
            List<Map<String, Object>> list = htTimeDao.selectTradeTimes();
            PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(list);
            message.setSuccess(true);
            message.setData(pageInfo);
            message.setCode("200");
            message.setMessage("操作成功");
        } catch (Exception e) {
            message.setCode("-1");
            message.setSuccess(false);
            message.setMessage("操作失败");
            e.printStackTrace();
        }
        return message;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public BusinessMessage insertTradeTime(Map<String, Object> params) {
        BusinessMessage message = new BusinessMessage();
        try {
            String time = LocalDateTime.now().format(DateTimeFormatter.ofPattern("HH:mm:ss"));
            Integer result = htTimeDao.thinkTime(time);
            if (0 == result) {
                PvTradeTime tradeTime = new PvTradeTime();
                tradeTime.setStartTime(new SimpleDateFormat("HH:mm:ss").format(Long.valueOf(params.get("startTime") + "")));
                tradeTime.setEndTime(new SimpleDateFormat("HH:mm:ss").format(Long.valueOf(params.get("endTime") + "")));
                tradeTime.setRemark((String) params.get("remark"));
                String now = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
                tradeTime.setCreateTime(now);
                tradeTime.setUpdateTime(now);
                pvTradeTimeMapper.insert(tradeTime);
                //redis操作
                message.setSuccess(true);
                message.setCode("200");
                message.setMessage("操作成功");
            } else {
                message.setCode("-1");
                message.setSuccess(false);
                message.setMessage("交易时间，不能操作");
                return message;
            }
        } catch (Exception e) {
            message.setCode("-1");
            message.setSuccess(false);
            message.setMessage("操作失败");
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            e.printStackTrace();
        }
        return message;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public BusinessMessage deleteTradeTime(Map<String, Object> params) {
        BusinessMessage message = new BusinessMessage();
        try {
            String time = LocalDateTime.now().format(DateTimeFormatter.ofPattern("HH:mm:ss"));
            Integer result = htTimeDao.thinkTime(time);
            if (0 == result) {
                QueryWrapper queryWrapper = new QueryWrapper();
                queryWrapper.setEntity(new PvTradeTime() {{
                    setId(Integer.valueOf( params.get("id")+""));
                }});
                PvTradeTime tradeTime = pvTradeTimeMapper.selectOne(queryWrapper);
                if (null != tradeTime) {
                    queryWrapper.setEntity(tradeTime);
                    pvTradeTimeMapper.delete(queryWrapper);
                    message.setSuccess(true);
                    message.setCode("200");
                    message.setMessage("操作成功");
                } else {
                    message.setCode("-1");
                    message.setSuccess(false);
                    message.setMessage("操作失败");
                }
            } else {
                message.setCode("-1");
                message.setSuccess(false);
                message.setMessage("交易时间，不能操作");
                return message;
            }
        } catch (Exception e) {
            message.setCode("-1");
            message.setSuccess(false);
            message.setMessage("操作失败");
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            e.printStackTrace();
        }
        return message;
    }

    @Override
    public boolean thinkTime() {
        boolean flag = false;
        String time = LocalDateTime.now().format(DateTimeFormatter.ofPattern("HH:mm:ss"));
        Integer result = htTimeDao.sellThinkTime(time);
        if (0 == result) {
            flag = false;
        } else {
            flag = true;
        }
        return flag;
    }


}
